package com.inspirationandgenius.media;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.lifeofdata.inspirationandgenius.R;

public class StreamingMedia {

	private ProgressBar playSeekBar;

    private ImageButton buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;
    

    public MediaPlayer getPlayer() {
		return player;
	}

	public StreamingMedia(ProgressBar playSeekBar, ImageButton buttonPlay) {
		super();
		this.playSeekBar = playSeekBar;
		this.buttonPlay = buttonPlay;
	}

//	public void onClick(View v) {
//        if (v == buttonPlay) {
//            startPlaying();
//        } else if (v == buttonStopPlay) {
//            stopPlaying();
//        }
//    }

    private void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }


    private void initializeMediaPlayer(String url) {
        player = new MediaPlayer();
        try {
            player.setDataSource(url);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    public void Pause() {
        if (player.isPlaying()) {
            player.stop();
        }
    }
}
